package com.frozenlab.android.wheretoeatai;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class FoodPlaceList extends ListActivity {

    private static final int ACTIVITY_CREATE=0;
    private static final int ACTIVITY_SUMMARY=1;

    private static final int INSERT_ID = Menu.FIRST;
    
    private FoodPlaceDbHelper dbHelper;
       
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.food_place_list);        
        dbHelper = new FoodPlaceDbHelper(this);
        dbHelper.open();
        listRecord();
        registerForContextMenu(getListView());
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.menu_insert);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
            case INSERT_ID:
                createRecord();
                return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }
    
    private void listRecord() {
        // Get all of the rows from the database and create the item list
    	Cursor cursor = dbHelper.readAllRecords();
        startManagingCursor(cursor);

        // Create an array to specify the fields we want to display in the list (such as KEY_TITLE or KEY_NAME)
        String[] from = new String[]{FoodPlaceDbHelper.KEY_NAME};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.name};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter adapter = 
            new SimpleCursorAdapter(this, R.layout.food_place_row, cursor, from, to);
        setListAdapter(adapter);
    }
    
    public void onAddClick(View button){
        Intent intent = new Intent(this, FoodPlaceAdd.class);
    	startActivity(intent);  
    }
    
    public void onBackClick(View button){	
    	FoodPlaceList.this.onBackPressed();
    }
    
    private void createRecord() {
        Intent i = new Intent(this, FoodPlaceAdd.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent indent = new Intent(this, FoodPlaceSummary.class);
        indent.putExtra(FoodPlaceDbHelper.KEY_ROWID, id);
        startActivityForResult(indent, ACTIVITY_SUMMARY);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        listRecord();
    }
}
